package edu.je.column.service;

import com.junqson.je.common.annotation.LoginRequire;
import com.junqson.je.common.model.ApiResult;
import com.junqson.je.common.model.vo.SimpleUserInfo;
import com.junqson.je.common.util.AppUtil;
import edu.je.column.model.AuthorInfoModel;
import edu.je.column.model.ColumnModel;
import edu.je.column.model.NewsModel;
import edu.je.column.model.SubscribeStateModel;
import edu.je.column.model.Vo.ColumnVo;
import edu.je.column.model.Vo.ColumnWithoutContentVo;
import edu.je.column.repo.AuthorInfoRepo;
import edu.je.column.repo.ColumnInfoRepo;
import edu.je.column.repo.ColumnRepo;
import edu.je.column.repo.SubscribeStateRepo;
import edu.je.column.util.PageUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author MengXin
 * @description:
 * @program: je-gateway
 * @date 2021/4/22 19:18
 * @mail mx@radish.cloud
 */
@Service
public class ColumnService {
    @Autowired
    ColumnRepo columnRepo;
    @Autowired
    AuthorInfoRepo authorInfoRepo;
    @Autowired
    ColumnInfoRepo columnInfoRepo;
    @Autowired
    SubscribeStateRepo subscribeStateRepo;
    public List<ColumnWithoutContentVo> getColmunList(Integer page,Integer size,String Section){
        Sort sort = Sort.by(Sort.Direction.DESC, "postTime");
        Pageable pageable = PageUtil.getPageable(page, size, sort);
        Page<ColumnModel> models;
        if ("undefined".equals(Section)||"全部".equals(Section)){
            models = columnRepo.findAll(pageable);
        }else {
            models = columnRepo.findAllBySection(Section, pageable);
        }
        return models.getContent().stream().map(this::convertToColumnWithoutContentVo).collect(Collectors.toList());

    }

    public ColumnVo getColumnDetailById(Long columnId){
        ColumnModel columnModel = columnRepo.findById(columnId).get();
        return convertToColumnVo(columnModel);
    }


    public Integer getSubscribeStateById(Long userId,Long columnId){
        Optional<SubscribeStateModel> opt = subscribeStateRepo.findByUserIdAndColumnId(userId,columnId);
        if (opt.isPresent()){
            return 1;
        }else {
            return 0;
        }
    }


    public SubscribeStateModel subscribColumnById(Long userId,Long columnId){
        Optional<SubscribeStateModel> opt = subscribeStateRepo.findByUserIdAndColumnId(userId,columnId);
        if (!opt.isPresent()){
            SubscribeStateModel subscribeStateModel = new SubscribeStateModel();
            subscribeStateModel.setUserId(userId);
            subscribeStateModel.setColumnId(columnId);
            subscribeStateModel.setSubscribeState(1);
            subscribeStateRepo.save(subscribeStateModel);
            return subscribeStateModel;
        }else{
            return opt.get();
        }
    }

    public ColumnVo convertToColumnVo(ColumnModel columnModel) {
        if (columnModel == null) {
            return null;
        }
        ColumnVo columnVo = new ColumnVo();
        AppUtil.copyPropertiesIgnoreNull(columnModel, columnVo);
        Long authorId = columnModel.getAuthorId();
        Long columnId = columnModel.getColumnId();
        columnVo.setAuthorInfoModel((authorInfoRepo.findById(authorId)).get());
        columnVo.setColumnInfoModel(columnInfoRepo.findById(columnId).get());
        return columnVo;
    }

    public ColumnWithoutContentVo convertToColumnWithoutContentVo(ColumnModel columnModel) {
        if (columnModel == null) {
            return null;
        }
        ColumnWithoutContentVo columnVo = new ColumnWithoutContentVo();
        AppUtil.copyPropertiesIgnoreNull(columnModel, columnVo);
        Long authorId = columnModel.getAuthorId();
        columnVo.setAuthorInfoModel((authorInfoRepo.findById(authorId)).get());
        return columnVo;
    }
}
